package com.yu.springboot_project_yu.service.impl;

import com.yu.springboot_project_yu.entity.User;
import com.yu.springboot_project_yu.repository.UserRepository;
import com.yu.springboot_project_yu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository userRepository;

    @Override
    public User save(User user) {
        return userRepository.save(user);
    }

    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public User findById(Long id) {
        return userRepository.findById(id).orElse(null);
    }

    // 重写deleteById方法，根据id删除用户
    @Override
    public void deleteById(Long id) {
        // 调用userRepository的deleteById方法，根据id删除用户
        userRepository.deleteById(id);
    }

    // 重写findByUsername方法，根据用户名查找用户
    @Override
    public User findByUsername(String username) {
        // 调用userRepository的findByUsername方法，根据用户名查找用户
        return userRepository.findByUsername(username);
    }
}